//
//  HYFStatusToolBarView.swift
//  MyWeibo
//
//  Created by 黄勇锋 on 16/7/24.
//  Copyright © 2016年 黄勇锋. All rights reserved.
//

import UIKit

class HYFStatusToolBarView: UIView {
    
    //转发,评论,赞
    var retweetButton:UIButton?
    var commentButton:UIButton?
    var unlikeButton:UIButton?
    
    //定义一个属性供外界赋值
    var statusViewModel:HYFStatusViewModel?{
        
        didSet{
         //赋值
            retweetButton?.setTitle(statusViewModel?.repostsCountStr, forState: .Normal)
            commentButton?.setTitle(statusViewModel?.commentCountStr, forState: .Normal)
            unlikeButton?.setTitle(statusViewModel?.attitudesCountStr, forState: .Normal)
        }
    }
    

    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setupUI(){
        
        //添加控件
         retweetButton = addChildButton("转发", imgName:"timeline_icon_retweet")
         commentButton = addChildButton("评论", imgName: "timeline_icon_comment")
         unlikeButton = addChildButton("赞", imgName: "timeline_icon_unlike")
        
        let line1 = addChildLine()
        let line2 = addChildLine()
        
        //添加约束
        retweetButton!.snp_makeConstraints { (make) in
            
            make.top.leading.bottom.equalTo(self)
            make.width.equalTo(commentButton!)
        }
        
        commentButton!.snp_makeConstraints { (make) in
            
            make.top.bottom.equalTo(self)
            make.leading.equalTo(retweetButton!.snp_trailing)
            make.width.equalTo(unlikeButton!)
            
        }
        
        unlikeButton!.snp_makeConstraints { (make) in
            
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(commentButton!.snp_trailing)
        }
        
        line1.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(retweetButton!.snp_trailing)
            make.centerY.equalTo(self)
        }
        
        line2.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(commentButton!.snp_trailing)
            make.centerY.equalTo(self)
        }
        
    }
    
    //添加分割线的方法
    func addChildLine()->UIImageView{
        
       let img = UIImageView(imgName: "timeline_card_bottom_line")
        //添加
        addSubview(img)
        //返回
        return img
    }

    //抽取出 一个添加button 的方法
    func addChildButton(title:String,imgName:String) -> UIButton{
        
        let button = UIButton()
        button.setTitle(title, forState: UIControlState.Normal)
        //字体颜色,字号
        button.setTitleColor(UIColor.blackColor(), forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        button.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
        
        //背景图片
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background" ), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: .Highlighted)
        
        //添加button
        addSubview(button)

        return button
    }
}
